package com.wutuo.child.provider.dao;


import cn.apestar.util.CommonUtil;
import com.wutuo.child.api.request.ChildRequest;
import com.wutuo.child.api.request.ListChildRequest;
import org.apache.ibatis.jdbc.SqlBuilder;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 孩子 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */

public class ChildSqlProvider extends BaseChildSqlProvider {

    public String lisForPage(ListChildRequest request) {

        BEGIN();
        getSelected();
        FROM("child a ");

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String getParentByChildId(Map<String, Object> param) {

        BEGIN();
        SqlBuilder.SELECT(
            " distinct a.id,a.account,a.name ,a.phone,a.ring_id as ringId,group_concat(distinct b.roster_id) as rosterIds");
        SqlBuilder.FROM("user a");
        SqlBuilder.INNER_JOIN("child b on a.id = b.user_id ");

        if (param.get("rosterIds") != null) {

            SqlBuilder.WHERE(
                "b.roster_id in (" + CommonUtil.getInString(param.get("rosterIds").toString())
                    + ")");
        }
        SqlBuilder.WHERE(" b.del_flag = true");
        SqlBuilder.WHERE(" a.del_flag = true");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        sql.append("group by a.id");
        return sql.toString();
    }

    public String listRollCallTaskResponsePage(ListChildRequest request) {

        BEGIN();
        SELECT(
            "a.task_name as taskName,b.create_date as taskTime,b.state,a.create_by as teacherId,b.user_id as id ");
        FROM(" task a, task_detail b");

        SqlBuilder.WHERE(" a.id = b.task_id ");
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getState() != null && request.getState() != null) {
            sql.append(" b.state = #{state,jdbcType=VARCHAR} ");
        }

        if (request.getUserId() != null && request.getUserId() != null) {
            sql.append(" b.user_id = #{userId,jdbcType=VARCHAR} ");
        }

        sql.append(" order by b.create_date desc ");
        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String list(ChildRequest request) {

        BEGIN();
        getSelected();
        FROM("child a");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {

        SELECT(
            "institution_id as cmInstitutionId,institution_id as institutionId,user_id as userId,roster_id as rosterId,create_by as createBy,create_date as createDate,update_by as updateBy,update_date as updateDate,remarks as remarks,del_flag as delFlag");
    }

    public void getComonJoin(ChildRequest request) {

        if (request.getId() != null) {
            SqlBuilder.WHERE(" a.id =#{id}");
        }

        if (request.getInstitutionId() != null) {
            SqlBuilder.WHERE(" a.institution_id = #{institutionId}");
        }

        if (request.getUserId() != null) {
            SqlBuilder.WHERE(" a.user_id = #{userId}");
        }

        if (request.getRosterId() != null) {
            SqlBuilder.WHERE(" a.roster_id = #{rosterId}");
        }

        if (request.getState() != null) {
            SqlBuilder.WHERE(" a.state = #{state}");
        }
    }

}